package com.atguigu.gulimall.order.config;

import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariPool;
import io.seata.rm.datasource.DataSourceProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

@Configuration
public class MySeataConfig {
    // 数据源
    @Autowired
    DataSourceProperties dataSourceProperties;

    /**
     * 创建分布式事务数据源
     * @param dataSourceProperties
     * @return
     */
    @Bean
    public DataSource dataSource(DataSourceProperties dataSourceProperties){
        //properties.initializeDataSourceBuilder().type(type).build();
        // 创建数据源
        HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
        // 设置数据源属性
        if (StringUtils.hasText(dataSourceProperties.getName())) {
            // 设置数据源名称
            dataSource.setPoolName(dataSourceProperties.getName());
        }
        //数据源包装
        return new DataSourceProxy(dataSource);
    }
}
